package Game

import (
	"GameGo/po"
	"GameGo/tool"
	"fmt"
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/text"
	"image/color"
	"math"
)

func DrawGame(screen *ebiten.Image, g *Game) {
	DrawPlayer(screen, g)
	DrawBullets(screen, g)
	DrawEnemies(screen, g)
	DrawUI(screen, g)
}

func DrawUI(screen *ebiten.Image, g *Game) {
	msg := fmt.Sprintf("得分: %d", g.Score)
	hp := fmt.Sprintf("HP：  %d / %d", g.PlayerHP, g.PlayerMaxHP)
	exp := fmt.Sprintf("EXP：  %d / %d", g.PlayerExp, g.PlayerMaxExp)
	text.Draw(screen, msg, tool.GetFont(8), 0, 60, color.White)
	text.Draw(screen, exp, tool.GetFont(8), 0, 80, color.White)
	text.Draw(screen, hp, tool.GetFont(8), 0, 40, color.White)
	text.Draw(screen, "[A]左[D]右", tool.GetFont(8), 0, 100, color.White)
}

func DrawPlayer(screen *ebiten.Image, g *Game) {

	playerH := float64(g.PlayerImg.Bounds().Dy()) / 2
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(g.PlayerX, g.PlayerY+playerH+6) // 后平移
	screen.DrawImage(g.PlayerTrailImg, op)

	opts := &ebiten.DrawImageOptions{}
	opts.GeoM.Translate(g.PlayerX, g.PlayerY)
	screen.DrawImage(g.PlayerImg, opts)
	//tool.DrawPlayerHPBar(screen, g.PlayerHP, g.PlayerMaxHP)
	g.Player.DrawPlayerHPBar(screen)

}

func DrawBullets(screen *ebiten.Image, g *Game) {
	for _, b := range g.Bullets {
		opt := &ebiten.DrawImageOptions{}
		opt.GeoM.Scale(b.Scale, b.Scale)
		if b.BulletType == po.EnemyBullet {
			opt.GeoM.Translate(b.Position[0], b.Position[1])
			screen.DrawImage(g.EnemyBulletImg, opt)
		} else {
			// 注意 Y 坐标向下，Y 分量取反
			angle := math.Atan2(b.Dir[1], b.Dir[0])
			//w, _ := g.BulletImg.Bounds().Dx(), g.BulletImg.Bounds().Dy()
			opt.GeoM.Rotate(-(angle - math.Pi/2))
			opt.GeoM.Translate(b.Position[0], b.Position[1])
			screen.DrawImage(g.BulletImg, opt)
		}
	}
}
func DrawEnemies(screen *ebiten.Image, g *Game) {
	enemyW := float64(g.EnemyImg.Bounds().Dx())
	enemyH := float64(g.EnemyImg.Bounds().Dy())
	for _, e := range g.Enemies {
		// 每帧绘制拖尾
		ex, ey, _, _ := e.EnemyOption(po.EnemyPosition, 0)
		HP, maxHP, _, _ := e.EnemyOption(po.Exp, 0)
		op := &ebiten.DrawImageOptions{}
		op.GeoM.Rotate(math.Pi)
		op.GeoM.Translate(ex+enemyW, ey+enemyH/2) // 后平移
		screen.DrawImage(g.EnemyTrailImg, op)

		opt := &ebiten.DrawImageOptions{}
		opt.GeoM.Translate(ex, ey)
		screen.DrawImage(g.EnemyImg, opt)
		tool.DrawEnemyHPBar(screen, ex, ey-6, int(HP), int(maxHP), enemyW, 4.0)

	}

}
